﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using hedefgrup.reports.renault.database;

namespace hedefgrup.reports.common
{
    public class Report_Dealer_Renault_Dacia : Report_Abstract
    {
        protected override void SetCriteriaList()
        {
            Criterias.Add(new Criteria_Roll() { Context = Context });
            Criterias.Add(new Criteria_EndMonth() { Context = Context });
            Criterias.Add(new Criteria_Network() { Context = Context });
            Criterias.Add(new Criteria_Organization() { Context = Context });
        }

        private void AppendDelaer(ReportResult results, DataRow delaerRow, float s2_1, float s4_1, float s41_1, float s6_1, float s7_1, float S7a_1, float s8_9, float s8_10, float s17_1, float s18_1, float s8b_1, float count)
        {
            Row dealerRow = new Row();
            results.Rows.Add(dealerRow);
            dealerRow.Cells.Add(new DealerCodeCell() { Text = delaerRow[base.DealerColumn].ToString().Trim(), TextAlign = "left" });
            dealerRow.Cells.Add(new Cell() { Text = base.GetDealerName(delaerRow[base.DealerColumn].ToString().Trim()), TextAlign = "left" });
            if (CoordinatorsReported)
            {
                dealerRow.Cells.Add(new Cell() { Text = delaerRow["KOORDINATOR"].ToString() });
            }
            dealerRow.Cells.Add(new Cell() { Text = delaerRow[AreaColumn].ToString() });
            dealerRow.Cells.Add(new Cell() { Text = delaerRow[base.DealerColumn].ToString() });
            dealerRow.Cells.Add(new Cell() { Text = count.ToString() });
            float s18Value = 100 * (s18_1 / count);
            float s17Value = 100 * (s17_1 / count);
            float s2Value = 100 * (s2_1 / count);
            float testDrive_15 = (s4_1 + s41_1) / count;
            float customerCall_15 = s7_1 / count;
            float explanationsSatisfaction_20 = s6_1 / count;
            float salesPersonSatisfaction_50 = (s8_9 + s8_10) / count;

            if (Context.CompatibleProjectName == Global.PROJECT_QVN2013)
            {
                dealerRow.Cells.Add(new Cell() { Text = string.Format("{0:0.0}", s18Value), Color = s18Value >= Global.TRESHOLD_RECOMMEND_RENAULT ? "Green" : "Red" });
                dealerRow.Cells.Add(new Cell() { Text = string.Format("{0:0.0}", s17Value), Color = s17Value <= Global.TRESHOLD_CALL_DEALER ? "Green" : "Red" });
            }
            else
            {
                float overallWeightedScore =
                    testDrive_15 * 0.40f +
                    customerCall_15 * 0.20f +
                    // explanationsSatisfaction_20 * 0.2f +
                    salesPersonSatisfaction_50 * 0.40f;

                dealerRow.Cells.Add(new Cell() { Text = string.Format("{0:0.00}", 100 * overallWeightedScore), Bold = true });
            }
        }

        public override ReportResult GenerateReport()
        {
            string columnNames = Context.CompatibleProjectName == Global.PROJECT_QVN2013 ? "S18, S17" : "S2, S4_1, S4, S6, S7, S7A, S8, S8b";
            DataTable rawData = db.query
            (
                "SELECT AY, YIL, " + base.DealerColumn + ", KOORDINATOR, " + AreaColumn + ", CNT = COUNT(*), " + columnNames + " FROM " + Context.TableName + " " +
                GetSqlStatementFilter() +
                " GROUP BY AY, YIL, " + base.DealerColumn + ", KOORDINATOR, " + AreaColumn + ", " + columnNames +
                " ORDER BY " + base.DealerColumn + " ASC",
                base.GetSqlStatementParameters()
            );

            ReportResult results = new ReportResult(this);
            Row headerRow = new Row();
            results.HeaderRows.Add(headerRow);
            headerRow.Cells.Add(new Cell() { Text = "Bayii Kodu" });
            headerRow.Cells.Add(new Cell() { Text = "Bayii Adı" });
            if (CoordinatorsReported)
            {
                headerRow.Cells.Add(new Cell() { Text = "Koordinatör" });
            }
            headerRow.Cells.Add(new Cell() { Text = "Bölge" });
            headerRow.Cells.Add(new Cell() { Text = "Bayii Kodu" });
            headerRow.Cells.Add(new Cell() { Text = "Baz" });
            if (Context.CompatibleProjectName == Global.PROJECT_QVN2013)
            {
                headerRow.Cells.Add(new Cell() { Text = "S18 - Tavsiye" });
                headerRow.Cells.Add(new Cell() { Text = "S17 - Geri Dönüş Oranı" });
            }
            else
            {
                headerRow.Cells.Add(new Cell() { Text = "Ağırlıklı Skor" });
            }

            string currentDealer = null;
            string newDealer = null;
            DataRow lastRow = null;

            float s18_1 = 0;
            float s17_1 = 0;
            float s2_1 = 0;
            float s4_1 = 0;
            float s41_1 = 0;
            float s6_1 = 0;
            float s7_1 = 0;
            float s7a_1 = 0;
            float s8_9 = 0;
            float s8_10 = 0;
            float s8b_1 = 0;
            float s18_1_total = 0;
            float s17_1_total = 0;
            float s2_1_total = 0;
            float s4_1_total = 0;
            float s41_1_total = 0;
            float s6_1_total = 0;
            float s7_1_total = 0;
            float s7a_1_total = 0;
            float s8_9_total = 0;
            float s8_10_total = 0;
            float s8b_1_total = 0;

            float count = 0;
            float count_total = 0;

            foreach (DataRow row in rawData.Rows)
            {
                newDealer = row[base.DealerColumn].ToString();

                if (currentDealer == null)
                {
                    currentDealer = row[base.DealerColumn].ToString();
                }

                if (currentDealer != newDealer)
                {
                    AppendDelaer(results, lastRow, s2_1, s4_1, s41_1, s6_1, s7_1, s7a_1, s8_9, s8_10, s17_1, s18_1, s8b_1, count);

                    count_total += count;
                    s18_1_total += s18_1;
                    s17_1_total += s17_1;
                    s2_1_total += s2_1;
                    s4_1_total += s4_1;
                    s41_1_total += s41_1;
                    s6_1_total += s6_1;
                    s7_1_total += s7_1;
                    s7a_1_total += s7a_1;
                    s8_9_total += s8_9;
                    s8_10_total += s8_10;
                    s8b_1_total += s8b_1;

                    count = 0;
                    s18_1 = 0;
                    s17_1 = 0;
                    s2_1 = 0;
                    s4_1 = 0;
                    s41_1 = 0;
                    s6_1 = 0;
                    s7_1 = 0;
                    s7a_1 = 0;
                    s8_9 = 0;
                    s8_10 = 0;
                    s8b_1 = 0;

                    currentDealer = newDealer;
                }

                int s17 = Context.CompatibleProjectName == Global.PROJECT_QVN2013 ? Convert.ToInt32(row["S17"]) : 0;
                int s18 = Context.CompatibleProjectName == Global.PROJECT_QVN2013 ? Convert.ToInt32(row["S18"]) : 0;
                int s2 = Context.CompatibleProjectName == Global.PROJECT_SATIS_SUREC ? row["S2"] != DBNull.Value ? Convert.ToInt32(row["S2"]) : 0 : 0;
                int s4 = Context.CompatibleProjectName == Global.PROJECT_SATIS_SUREC ? row["S4"] != DBNull.Value ? Convert.ToInt32(row["S4"]) : 0 : 0;
                int s41 = Context.CompatibleProjectName == Global.PROJECT_SATIS_SUREC ? row["S4_1"] != DBNull.Value ? Convert.ToInt32(row["S4_1"]) : 0 : 0;
                int s6 = Context.CompatibleProjectName == Global.PROJECT_SATIS_SUREC ? row["S6"] != DBNull.Value ? Convert.ToInt32(row["S6"]) : 0 : 0;
                int s7 = Context.CompatibleProjectName == Global.PROJECT_SATIS_SUREC ? row["S7"] != DBNull.Value ? Convert.ToInt32(row["S7"]) : 0 : 0;
                int s7a = Context.CompatibleProjectName == Global.PROJECT_SATIS_SUREC ? row["S7A"] != DBNull.Value ? Convert.ToInt32(row["S7A"]) : 0 : 0;
                int s8 = Context.CompatibleProjectName == Global.PROJECT_SATIS_SUREC ? row["S8"] != DBNull.Value ? Convert.ToInt32(row["S8"]) : 0 : 0;
                int s8b = Context.CompatibleProjectName == Global.PROJECT_SATIS_SUREC ? row["S8b"] != DBNull.Value ? Convert.ToInt32(row["S8b"]) : 0 : 0;
                int cnt = Convert.ToInt32(row["CNT"]);

                count += cnt;
                if (s17 == 1) s17_1 += cnt;
                if (s18 == 1) s18_1 += cnt;
                if (s2 == 1) s2_1 += cnt;
                if (s4 == 1) s4_1 += cnt;
                if (s41 == 1) s41_1 += cnt;
                if (s6 == 1) s6_1 += cnt;
                if (s7 == 1) s7_1 += cnt;
                if (s7a == 1) s7a_1 += cnt;
                if (s8 == 9) s8_9 += cnt;
                if (s8 == 10) s8_10 += cnt;
                if (s8b == 1) s8b_1 += cnt;

                lastRow = row;
            }

            if (lastRow != null)
            {
                AppendDelaer(results, lastRow, s2_1, s4_1, s41_1, s6_1, s7_1, s7a_1, s8_9, s8_10, s17_1, s18_1, s8b_1, count);
            }

            count_total += count;
            s18_1_total += s18_1;
            s17_1_total += s17_1;
            s2_1_total += s2_1;
            s4_1_total += s4_1;
            s41_1_total += s41_1;
            s6_1_total += s6_1;
            s7_1_total += s7_1;
            s7a_1_total += s7a_1;
            s8_9_total += s8_9;
            s8_10_total += s8_10;
            s8b_1_total += s8b_1;

            if (count_total > 0)
            {
                Row totalRow = new Row();
                results.Rows.Insert(0, totalRow);

                totalRow.Cells.Add(new Cell() { Text = "", TextAlign = "left" });
                totalRow.Cells.Add(new Cell() { Text = "GENEL", TextAlign = "left", Span = CoordinatorsReported ? 4 : 3 });
                totalRow.Cells.Add(new Cell() { Text = count_total.ToString() });
                float s18Value = 100 * (s18_1_total / count_total);
                float s17Value = 100 * (s17_1_total / count_total);
                float s2Value = 100 * (s2_1_total / count_total);
                float s4Value = s4_1_total / count_total;
                float s6Value = s6_1_total / count_total;
                float s7Value = s7_1_total / count_total;
                float s7aValue = s7a_1_total / count_total;
                float s8Value = (s8_9_total + s8_10_total) / count_total;
                float s8bValue = s8b_1_total / count_total;
                if (Context.CompatibleProjectName == Global.PROJECT_QVN2013)
                {
                    totalRow.Cells.Add(new Cell() { Text = string.Format("{0:0.0}", s18Value), Color = s18Value >= 91 ? "Green" : "Red" });
                    totalRow.Cells.Add(new Cell() { Text = string.Format("{0:0.0}", s17Value), Color = s17Value <= 25 ? "Green" : "Red" });
                }
                else
                {
                    // XXX
                    float testDrive_15 = (s4_1_total + s41_1_total) / count_total;
                    float customerCall_15 = s7_1_total / count_total;
                    float explanationsSatisfaction_20 = s6_1_total / count_total;
                    float salesPersonSatisfaction_50 = (s8_9_total + s8_10_total) / count_total;

                    float overallWeightedScore =
                    testDrive_15 * 0.40f +
                    customerCall_15 * 0.20f +
                    // explanationsSatisfaction_20 * 0.2f +
                    salesPersonSatisfaction_50 * 0.40f;

                    totalRow.Cells.Add(new Cell() { Text = string.Format("{0:0.00}", 100 * overallWeightedScore), Bold = true });
                }
            }

            return results;
        }

        public override string DisplayName
        {
            get { return "Bayii Raporu - Renault - Dacia"; }
        }

        public override bool SupportsProject(string projectName)
        {
            return base.SupportsProject(projectName) || projectName == Global.PROJECT_SATIS_SUREC;
        }

        public override string SortPrefix
        {
            get { return "0x06"; }
        }
    }
}
